/**
 * @info    (MenuController Klasse tbv. V&K simulatie, welke het menu rendered)
 * @author  (Veenstra Stephan, Bruins Marc, Jansma Michel) 
 * @version (2012.11.12)
 */

package controller;

import runner.*;
import model.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MenuController extends AbstractController {
		
	private JMenuBar bar;
	
	public MenuController(SimulatorRunner simulator) {
		//Roep super klasse aan
		super(simulator);
    	//Instantie variabele bar initialiseren
    	this.bar = new JMenuBar();
		this.setBar();
	}
	
	 /***************************************************************
     * V&K MENU DATA START
     **************************************************************/
    
    /**
     * @method addMenu
     * @params <String> title
     * @info   Voeg een menu toe aan een menubalk
     * @return <JMenu> menu
     */
    private JMenu addMenu(String title) {
    	JMenu menu = new JMenu(title);
    	this.getBar().add(menu);
    	return menu;
    }
    
    /**
     * @method addMenuItems
     * @params <JMenu> menu, <HashMap<String,JMenuItem> items
     * @info   Voeg menu items toe aan menu 
     * @return <JMenu> menu
     */
    private void addMenuItems(JMenu menu, HashMap<String, JMenuItem> items) {
		//Loop door menu items
		int index = 0;
		//Items iterator
		Iterator it = items.keySet().iterator();
		while(it.hasNext()) {
			//Menu item
			String key 	   = it.next().toString();
			JMenuItem item = items.get(key);
			//Leeg event handler params
			if(!super.getParams().isEmpty()) {
				super.clearAllParams();
			}
			//Voeg menu item toe aan params
			super.addParam("menu", item);
			//Item action listener
			item.addActionListener(super.getEventHandler(key));
			//Voeg item toe aan menu
			menu.add(item);
		}
    }
	
	/*******************************************
	 * MENU BAR DATA START
	 ******************************************/
	
    /**
     * @method setBar
     * @params null
     * @info   Render simulator menu balk 
     * @return null
     */
    private void setBar() {
    	//Maak menus aan
        JMenu menu1 = this.addMenu("Menu 1");
        JMenu menu2 = this.addMenu("Diagrams");
        JMenu help  = this.addMenu("Help");
        //Maak menu items aan
        HashMap<String, JMenuItem> items = new HashMap<String, JMenuItem>();
        items.put("testHandler", new JMenuItem("item"));
        //Voeg menu items toe en leeg hashmap
        this.addMenuItems(menu1, items);
        items.clear();
        items.put("pieViewRequestHandler", new JMenuItem("Verberg cirkeldiagram"));
        //Voeg menu items toe en leeg hashmap
        this.addMenuItems(menu2, items);
        items.clear();
        items.put("menuRequestHandler", new JMenuItem("HELP"));
        //Voeg menu items toe en leeg hashmap
        this.addMenuItems(help, items);
    }
	
	public JMenuBar getBar() {
		return this.bar;
	}
	
	/*******************************************
	 * PANEL DATA END
	 ******************************************/
	
}
